package cn.ttxs.lib.wiget;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import cn.ttxs.lib.R;
import cn.ttxs.lib.tool.ToolEmpty;


/**
 * Created by lenovo on 2017/6/28.
 */

public class CusLoading extends ProgressDialog {
    private TextView mTvLoading;
    private String text;
    private boolean canCancel;//点击加载框之外是否可以取消

    public CusLoading(Context context) {
        super(context);
    }

    public CusLoading(Context context, int theme, boolean canCancel) {
        super(context, theme);
        this.canCancel = canCancel;
    }

    public CusLoading(Context context, String text, boolean canCancel) {
        super(context);
        this.text = text;
        this.canCancel = canCancel;
    }

    public CusLoading(Context context, int theme, String text, boolean canCancel) {
        super(context, theme);
        this.text = text;
        this.canCancel = canCancel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(getContext(), canCancel);
    }

    private void init(Context context, boolean canCancel) {
        //设置不可取消，点击其他区域不能取消，实际中可以抽出去封装供外包设置
        if (canCancel) {
            setCancelable(true);
            setCanceledOnTouchOutside(true);
        } else {
            setCancelable(false);
            setCanceledOnTouchOutside(false);
        }
        setContentView(R.layout.customer_dialog);
        mTvLoading = (TextView) findViewById(R.id.tv_load_dialog);
        if (ToolEmpty.isNotEmpty(this.text)) {
            mTvLoading.setText(this.text);
        }
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    @Override
    public void dismiss() {
        try {
            super.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
